#include "mybutton.h"
#include <QDebug>
#include <QPropertyAnimation>
// mybutton::mybutton(QWidget *parent)
//     : QPushButton{parent}
// {}
mybutton::mybutton(QString normalImg,QString pressImg){
    normalImagPath=normalImg;
    pressedImgPath=pressImg;
    QPixmap pixmap;
    bool ret = pixmap.load(normalImagPath);
    if(!ret){
        qDebug()<<"加载图片失败";
    }
    this->setFixedSize(pixmap.width(),pixmap.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pixmap);
    this->setIconSize(QSize(pixmap.width(),pixmap.height()));

}
void mybutton::zoom1(){
    QPropertyAnimation * animation1 = new QPropertyAnimation(this,"geometry");
    animation1->setDuration(200);
    animation1->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    animation1->start();
}
void mybutton::zoom2(){
    QPropertyAnimation * animation1 =  new QPropertyAnimation(this,"geometry");
    animation1->setDuration(200);
    animation1->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    animation1->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation1->setEasingCurve(QEasingCurve::OutBounce);
    animation1->start();
}
void mybutton::mousePressEvent(QMouseEvent *e){
    if(pressedImgPath!=""){
        QPixmap pixmap;
        bool ret = pixmap.load(pressedImgPath);
        if(!ret){
            qDebug()<<pressedImgPath<<"加载图片失败";
            this->setFixedSize(pixmap.width(),pixmap.height());
            this->setStyleSheet("QPushButton{border:0px}");
            this->setIcon(pixmap);
            this->setIconSize(QSize(pixmap.width(),pixmap.height()));

        }
    }
    return QPushButton::mousePressEvent(e);
}

void mybutton::mouseReleaseEvent(QMouseEvent *e){
    if(normalImagPath != "") //选中路径不为空，显示选中图片
    {
        QPixmap pixmap;
        bool ret = pixmap.load(normalImagPath);
        if(!ret)
        {
            qDebug() << normalImagPath << "加载图片失败!";
        }
        this->setFixedSize( pixmap.width(), pixmap.height() );
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pixmap);
        this->setIconSize(QSize(pixmap.width(),pixmap.height()));
    }
    //交给父类执行 释放事件
    return QPushButton::mouseReleaseEvent(e);
}
